package com.ai.common;

import com.ai.common.constant.CommonConstant;
import com.ai.common.constant.enumP.ResultCode;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 通用响应对象
 *
 * @param <T>
 */

@Data
@Accessors(chain = true)
public class CommonResponse<T> implements Serializable {

    /**
     * 响应状态码
     */
    private Integer code = 0;

    /**
     * 响应提示消息
     */
    private String msg = "";

    /**
     * 响应obj对象
     */
    private T data;

    public CommonResponse(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> CommonResponse<T> ok() {
        return ok("SUCCESS");
    }

    public static <T> CommonResponse<T> ok(String msg) {
        CommonResponse<T> commonResponse = new CommonResponse<T>(CommonConstant.SC_OK_200, msg);
        return commonResponse;
    }

    public static <T> CommonResponse<T> ok(T data) {
        CommonResponse<T> commonResponse = new CommonResponse<T>(CommonConstant.SC_OK_200, "SUCCESS");
        commonResponse.setData(data);
        return commonResponse;
    }

    public static <T> CommonResponse<T> ok(String msg, T data) {
        CommonResponse<T> commonResponse = new CommonResponse<T>(CommonConstant.SC_OK_200, msg).setData(data);
        return commonResponse;
    }


    public static <T> CommonResponse<T> error(String msg, T data) {
        CommonResponse<T> commonResponse = new CommonResponse<T>(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg).setData(data);
        return commonResponse;
    }

    public static <T> CommonResponse<T> error(Integer code, String msg) {
        CommonResponse<T> commonResponse = new CommonResponse<T>(code, msg);
        return commonResponse;
    }

    public static <T> CommonResponse<T> error(String msg) {
        return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg);
    }

    public static <T> CommonResponse<T> result(ResultCode resultCode) {
        CommonResponse<T> commonResponse = new CommonResponse<T>(resultCode.getCode(), resultCode.getMessage());
        return commonResponse;
    }
}
